package com.txd.mofa.view;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.SpannableStringBuilder;
import android.text.method.LinkMovementMethod;
import android.text.style.ClickableSpan;
import android.text.style.ForegroundColorSpan;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.google.gson.JsonObject;
import com.kongzue.baseframework.util.AppManager;
import com.kongzue.baseframework.util.JumpParameter;
import com.kongzue.baseframework.util.Preferences;
import com.txd.mofa.R;
import com.txd.mofa.login.XieyiZhengceAty;

import java.lang.reflect.Field;
import java.util.Map;


/**
 * Created by bin on 2018/12/21.
 */

public class XieyiDialog extends Dialog implements View.OnClickListener {

    private String type;
    private SignListener signListener;
    private Context context;
    private TextView tv_quxiao, tv;
    private boolean flag;
    private String type_id;
    private String fenlei;

    private String s;

    private Activity activity;

    private String token;


    public XieyiDialog(@NonNull Context context, @NonNull SignListener signListener) {
        super(context, R.style.share_style);
        this.signListener = signListener;
        this.context = context;
        activity = (Activity) context;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.xieyi_dialog);

        Window window = this.getWindow();

        //解决 状态栏变色的bug
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
            window.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            window.setStatusBarColor(Color.TRANSPARENT);
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
                try {
                    Class decorViewClazz = Class.forName("com.android.internal.policy.DecorView");
                    Field field = decorViewClazz.getDeclaredField("mSemiTransparentStatusBarColor");
                    field.setAccessible(true);
                    field.setInt(getWindow().getDecorView(), Color.TRANSPARENT);  //去掉高版本蒙层改为透明
                } catch (Exception e) {
                }
            }
        }

        Preferences.getInstance().set(context,"flag","flag","2");


        findViewById(R.id.tv_no).setOnClickListener(this);
        findViewById(R.id.tv_yes).setOnClickListener(this);
        TextView text = findViewById(R.id.tv_text);

        setColor(text, text.getText().toString());
//        setColor1(text, text.getText().toString());
    }



    /**
     * 根据手机的分辨率从 dp 的单位 转成为 px(像素)
     */
    public static int dip2px(Context mContext, float dpValue) {
        final float scale = mContext.getResources().getDisplayMetrics().density;
        return (int) (dpValue * scale + 0.5f);
    }

    private SpannableString elipseString, elipseString1;


    private void setColor(TextView tv, String content) {
        SpannableStringBuilder spannable = new SpannableStringBuilder(content);

        spannable.setSpan(new ForegroundColorSpan(Color.parseColor("#d36cd2")),18, 24, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        spannable.setSpan(new ForegroundColorSpan(Color.parseColor("#d36cd2")),25, 31, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        tv.setMovementMethod(LinkMovementMethod.getInstance());
        spannable.setSpan(new TextClick(),18, 24 , Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        spannable.setSpan(new TextClick1(),25, 31 , Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        tv.setText(spannable);
    }
    private class TextClick extends ClickableSpan {
        @Override
        public void onClick(View widget) {
            //在此处理点击事件
//            jump(XieyiZhengceAty.class,new JumpParameter().put("pos",0).put("first","222"));
            Intent intent = new Intent(context,XieyiZhengceAty.class);
            intent.putExtra("pos",0);
            intent.putExtra("first","111");
            intent.putExtra("flag","111");
            context.startActivity(intent);
        }
    }
    private class TextClick1 extends ClickableSpan {
        @Override
        public void onClick(View widget) {
            //在此处理点击事件
            Intent intent = new Intent(context,XieyiZhengceAty.class);
            intent.putExtra("pos",1);
            intent.putExtra("first","111");
            intent.putExtra("flag","111");
            context.startActivity(intent);
        }
    }

    @Override
    public void onClick(View view) {

        switch (view.getId()) {
            case R.id.tv_no:
                ShanchuDialog1 shanchuDialog1 = new ShanchuDialog1(context, new ShanchuDialog1.SignListener() {
                    @Override
                    public void shanchu() {

                    }
                });
                shanchuDialog1.show();
//                Preferences.getInstance().set(context,"flag","flag","2");
//                AppManager.getInstance().killAllActivity();
                break;
            case R.id.tv_yes:
                signListener.dia();
                this.dismiss();
                break;
        }
    }

    public interface SignListener {
        void dia();
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
        Preferences.getInstance().set(context,"flag","flag","2");
        AppManager.getInstance().killAllActivity();
    }
}

